/**********************************************************************
 * Copyright (C) 2005-2016 All rights reserved.
 * FILENAME: 	 monitor.js
 * VERSION: 	 1.8
 * CREATED: 	 2016-06-07 10:20:24
 * MODIFIED:	2016-10-20
 * DESCRIPTION:  M渠道采集
 *
 **********************************************************************/

$(document).ready(function() {
	//通用全局：页面加载采集
	page_load_event();

	//商品详情页相关
	product_detail_action();
	//掌上医生相关
	mobile_doctor_action();
	//健客商城相关
	jk_mall_action();
	//订单相关
	order_page_action();

	////////////////////////////////////////////////////////////////////////////////

	/*
	 * 页面加载事件处理方法
	 * 注：有些页面加载参数需携带个性化参数
	 */
	function page_load_event() {
		var evtLoadPage = 0;

		var href = window.location.href;
		var reg_jkzx = new RegExp("/\\w+/\\d+\.htm");
		if (reg_jkzx.test(href) && href.indexOf("product") == -1) { //健康咨询页面访问次数统计
			//获取一级分类、二级分类、三级分类
			var first_class = $(".bodybg p.lne26 a").eq(-3).text();
			var second_class = $(".bodybg p.lne26 a").eq(-2).text();
			var third_class = $(".bodybg p.lne26 a").eq(-1).text();

			var child_a_cnt = $(".bodybg p.lne26").children('a').length;
			if (child_a_cnt >= 3) {
				if (child_a_cnt > 4) {
					first_class = $(".bodybg p.lne26 a").eq(1).text();
					second_class = $(".bodybg p.lne26 a").eq(2).text();
					third_class = $(".bodybg p.lne26 a").eq(3).text();
				}

				var other_params = {
					"first_class": first_class,
					"second_class": second_class,
					"third_class": third_class
				};

				collect_proc(evtLoadPage, other_params);
			}
		} else {
			//没有个性化参数的 Load，都在这里处理
			var other_params = {};
			collect_proc(evtLoadPage, other_params);
		}
	}
	////////////////////////////////////////////////////////////////////////////////

	/*
	 * 产品详情页点击行为收集
	 */
	function product_detail_action() {
		var evtFreeCallBack = "1000101";
		var evtTelText = "1000102";
		var evtTelImage = "1000103";
		var evtOnlineText = "1000104";
		var evtOnlineBuy = "1000105";
		var evtAddCart = "1000106";
		var evtImmBuy = "1000107";
		var evtImmBuyPack = "1000108";
		var evtAddCartInPack = "1000109";
		var evtImmBuyInPack = "1000110";
		var evtComboHot = "1000111";

		var href = window.location.href;
		var other_params = {};

		if (href.indexOf("combCode") >= 0) { //如果包含combCode则是套装详情页面
			//点击电话咨询
			$(".footer .clearfix a:first").click(function() {
				collect_proc(evtTelImage, other_params);
			});
			//点击立即购买
			$(".footer li:last a:first").click(function() {
				collect_proc(evtImmBuyInPack, other_params);
			});
			//在套餐页点击加入购物车次数
			$(".footer li:last a:last").click(function() {
				collect_proc(evtAddCartInPack, other_params);
			});
		} else if (href.indexOf("product") > -1) { //非组合页面套装,即商品详情页
			//点击电话咨询图片
			$(".footer .clearfix a:first").click(function() {
				collect_proc(evtTelImage, other_params);
			});

			//左上咨询电话文本
			$(".dhzx .clearfix li:first span").click(function() {
				collect_proc(evtTelText, other_params);
			});

			//免费回拨
			$(".tel_box .callback button").click(function() {
				var tel = $('#Tel_bot').val();
				if (tel == "") {
					return false;
				}
				var mobile_pattern = /^(\d{11}$)/;
				var tel_pattern = /^(\d{3}-\d{8}|\d{4}-\d{7})$/;
				if (mobile_pattern.exec(tel) || tel_pattern.exec(tel)) {
					collect_proc(evtFreeCallBack, other_params);
				}
			});

			//点击右上在线咨询文本
			$(".dhzx ul li:last a span").click(function() {
				collect_proc(evtOnlineText, other_params);
				setCookie(href, 1);
			});

			//点击立即购买
			$(".footer li:last a:first").click(function() {
				collect_proc(evtImmBuy, other_params);
				if (getCookie(href) == 1) {
					//在线咨询后立即购买
					collect_proc(evtOnlineBuy, other_params);
				}
			});

			//主产品页点击加入购物车
			$(".footer li:last a:last").click(function() {
				collect_proc(evtAddCart, other_params);
			});

			//在主产品页点击立即购买套餐
			$(".tcj .tzxq").click(function() {
				collect_proc(evtImmBuyPack, other_params);
			});

			//在主产品页上套装热区点区
			$(".pro_area").click(function() {
				collect_proc(evtComboHot, other_params);
			});
		}
	}

	/*
	 * 掌上医生点击行为收集
	 */
	function mobile_doctor_action() {
		var evtFloatLink = "1010101";
		var evtImmDown = "1010102";
		//var evtImmDownAck = "1010103";

		//来自健客问答的悬浮链接点击
		$('.app_open').click(function() {
			var href = window.location.href;
			if (href.indexOf('question') > -1 || href.indexOf('news') > -1 || href.indexOf('jibing') > -1) {
				var other_params = {};
				collect_proc(evtFloatLink, other_params);
			}
		});

		//掌上医生首页点击下载事件
		$('.now1w').click(function() {
			var href = window.location.href;
			if (href.indexOf('zsys') > -1) {
				var other_params = {};
				collect_proc(evtImmDown, other_params);
			}
		});

		$('.now2w').click(function() {
			var href = window.location.href;
			if (href.indexOf('zsys') > -1) {
				var other_params = {};
				collect_proc(evtImmDown, other_params);
			}
		});
	}

	/*
	 * 健客商城点击行为收集
	 */
	function jk_mall_action() {
		var evtHomeAds = "1020101";
		var evtProdDetailAds = "1020102";
		var evtImmSetup = "1020103";
		var evtImmDown = "1020104";
		//var evtDownAck = "1020105";
		var evtHbImmeDown = "1020106";
		//var evtHbImmeDownAck = "1020107";


		$('.app-box ul li:last a').click(function() {
			var href = window.location.href;
			if (href == 'http://m.jianke.com/') {//健客首页下载统计
				var other_params = {};
				collect_proc(evtHomeAds, other_params);
			}
		});

		$('.jk-box ul li:last a').click(function() {
			var href = window.location.href;
			if (href.indexOf('product') > -1) { //健客产品页下载统计
				var other_params = {};
				collect_proc(evtProdDetailAds, other_params);
			}
		});




		$('.btn_dl1').click(function() { //商城首页下载统计
			var href = window.location.href;
			if (href.indexOf('scapp') > -1) {
				var other_params = {};
				collect_proc(evtImmSetup, other_params);
			}
		});

		$('.btn_dl2').click(function() { //商城首页下载统计
			var href = window.location.href;
			if (href.indexOf('scapp') > -1) {
				var other_params = {};
				collect_proc(evtImmDown, other_params);
			}
		});

		$('.dl').click(function() { //健客分享红包页面健客商城APP下载统计
			var href = window.location.href;
			if (href.indexOf('hbfx2') > -1) {
				var other_params = {};
				collect_proc(evtHbImmeDown, other_params);
			}
		});
	}



	/**
	 * 成功的订单
	 */
	function order_page_action() {
		var href = window.location.href;

		var order_no = $('.ywcddr').children().eq(1).text();
		varorder_no = order_no.substring(4);

		if (href.indexOf('Success_Order') > -1) {
			var other_params = {
				"order_no": order_no
			};
			collect_proc(5, '0', other_params);
		}
	}

	////////////////////////////////////////////////////////////////////////////////

	/*
	 * 采集通用方法
	 */
	function collect_proc(event_type, other_params) {
		//生成UUID
		function gen_uuid() {
			var S4 = function() {
				return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
			};
			return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + "-aaaa");
		}

		var cookie = {};

		//写cookie
		cookie.set = function(n, v, t) {
			var exp = new Date();
			exp.setTime(exp.getTime() + (t || 24) * 60 * 60 * 1000 * 365);
			document.cookie = n + "=" + escape(v) + ";expires=" + exp.toGMTString() + ';path=/';
		};
		//读cookie
		cookie.get = function(n) {
			var arr = document.cookie.match(new RegExp("(^| )" + n + "=([^;]*)(;|$)"));
			if (arr != null) {
				return unescape(arr[2]);
			}
			return null;
		};

		var user_id = "";
		var strCookie = document.cookie;
		var arrCookie = strCookie.split("; ");
		var user_flag = "0";
		for (var k = 0; k < arrCookie.length; ++k) {
			var arr = arrCookie[k].split("=");
			if (arr[0] == "rights_uid") {
				user_flag = arr[1];
				userflag = "1";
			}
		}

		if (user_id == "") {
			var ck_flag = "bd_muid";
			var co = cookie.get(ck_flag);
			if (co != null) {
				user_id = cookie.get(ck_flag);
			} else {
				user_id = gen_uuid();
				cookie.set(ck_flag, user_id);
			}
		}

		//序列化个性化参数
		var jsn_str = "{ ";
		var tmp_id = 0;
		for (var item in other_params) {
			if (tmp_id++ > 0) {
				jsn_str += ",";
			}
			jsn_str += "'" + item + "':'" + other_params[item] + "'";
		}
		jsn_str += " }";

		var list = {};
		list["AppId"] = 2;
		list["EventType"] = event_type;
		list["UserId"] = user_id;
		list["UserFlag"] = user_flag;
		list["PageId"] = window.location.href;
		list["Referrer"] = document.referrer;
		list["Params"] = jsn_str;

		//推送
		$.ajax({
			type: "POST",
			url: "http://bdentry.jianke.com/bigeater/mcontrail",
			//url: "http://172.16.2.102/bigeater/mcontrail",
			data: list,

			//成功
			success: function(data) {},
			//出错
			error: function(e) {}
		});
	}

	//设置cookie一天
	function setCookie(name, value) {
		var Days = 1;
		var exp = new Date();
		exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
		document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
	}
	//读取cookie
	function getCookie(name) {
		var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
		if (arr = document.cookie.match(reg)) {
			return unescape(arr[2]);
		} else {
			return null;
		}
	}
});
